<?php
/**
 * 测试样例类
 *
 * PHP VERSION = PHP 7.2.20
 */
namespace joinpay;
require "Autoload.php";

use joinpay\Request;
use joinpay\SecretKey;
use joinpay\RequestUtil;
use joinpay\RandomUtil;
use joinpay\AESUtil;

$requestdata=$_REQUEST;



//平台公钥
$platPublicKey = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCl3h19MlofijMPj3RGYVyuwBxCifjSuqO5fPCpp+L3wkE8xyXAJ1Q18/EkKRzorcXiQmlnMCpyToJqygMHdokKQWYns4rImdXDRYyPFaqb4wl8XvANx/a3y6A9QXtJ2QTbzOGFX1k0vJ2M9JAzAb+2/PMomiQF1Zv4q40L23o+2wIDAQAB
-----END PUBLIC KEY-----";

//商户私钥
$mchPrivateKey = "-----BEGIN RSA PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANVdQUwKFFj5+E+TjPe+uqA/5tzBeBS5IxrDWvQ/v4KWAZ/Sby7+FPb7Wk8gCOhkTmLhJYwBuT2BOR93/wQMWkmul8Rs6mw6fej/ii38u8m8EzG6n+bBBELjfuSqFfSbM1cnTpU4dvd6LNVn8kMK6yXoHIzqINBZ6CckE9wjsCl5AgMBAAECgYAgmT/puzyCn6aFmfiWvYhMBZdjMRgEqoinhSN3X+oFrJ+Q6JjeKCnR1nNk1XMm9nRXfSi8bGL996lfmnjXCIAerEVo+sbgCdhgSGvY0UiLEzpw4zT3hq0W6WnAMuI+qdjHU3fAZLO+NdWTlDYiBydhWfAo/0gQw2rzSU6f9LSbAQJBAPizmCOH3f3JALqM2xe873NV3RaTuH9o1AOFrRlFMZKYkdXjp0y9Y0WnZRxevc8NmllV95O6vI3n7Ne6JY0Sn9kCQQDboC8/ZGAlvLOvEA7iMxOPPVCcOlXk1dAECAErQMF/TLyRYj1/oAo7bgJJb55b0DzNz25cyfChVlgaPepaQ3KhAkEAk9HjRpMV/ib/eOIJwyU4SZwqaaXpjkkXYvSMOy3mVhS68Z97K+CRZbAmJ+Oj+7JBIII5QiRGuC22l057zUrZmQJAFOlLll2YSf+NnD2LLT5vvlO4OSfUxdUV2Uxp0BR6WASWb8FK2bwDPWVcEmHuUg+hunblqzAPGU1/VgBPMAj8AQJBAKQZXqaxtguRUq5k7g1q0dpzfEjxuHXJ/RHWZwbs3usBkUF7ThZ2JrEj1PWJLrQsDQtcdkaogY+6P6Y1MS56JYs=
-----END RSA PRIVATE KEY-----";

$secKey = RandomUtil::randomStr(16);



$data = [];
$data["mch_no_trade"] = "";

$data["mch_order_no"] = !empty($requestdata['mch_order_nos'])?$requestdata['mch_order_nos']:"";
$data["order_amount"] = !empty($requestdata['order_amount'])?$requestdata['order_amount']:"";

$data["mch_req_time"] = date('Y-m-d H:i:s', time());
 
$data["order_desc"] = !empty($requestdata['order_desc'])?$requestdata['order_desc']:"";

$data["id_type"] = "1";

$data["callback_url"] = 'http://mpjx.phasercn.com/api/notify';



$data["callback_param"] = !empty($requestdata['callback_param'])?$requestdata['callback_param']:"";

if(empty($requestdata['payer_name'])){
    $requestdata['payer_name']='';
}
if(empty($requestdata['id_no'])){
    $requestdata['id_no']='';
 }
 if(empty($requestdata['bank_card_no'])){
    $requestdata['bank_card_no']='';
 }
 if(empty($requestdata['mobile_no'])){
    $requestdata['mobile_no']='';
 }


$data["payer_name"] = AESUtil::encryptECB($requestdata['payer_name'], $secKey);//加密


$data["id_no"] = AESUtil::encryptECB($requestdata['id_no'], $secKey);//加密

$data["bank_card_no"] = AESUtil::encryptECB($requestdata['bank_card_no'], $secKey);//加密

$data["mobile_no"] = AESUtil::encryptECB($requestdata['mobile_no'], $secKey);//加密


$request = new Request();

$ispaid = !empty($requestdata['is_paid'])?$requestdata['is_paid']:"0";

if($ispaid==1){
  //验证过的直接发送支付短信
  $request->setMethod("fastPay.agreement.paySms");
}else{
  $request->setMethod("fastPay.agreement.signSms");


}
//$request->setMethod("fastPay.agreement.smsSign");

$request->setVersion("1.0");
$request->setMchNo("888111200001323");
$request->setSignType("2");
$request->setRandStr(RandomUtil::randomStr(32));
$request->setData($data);
$request->setSecKey($secKey);//rsa有效

$secretKey = new SecretKey();
$secretKey->setReqSignKey($mchPrivateKey);//签名：使用商户私钥
$secretKey->setRespVerifyKey($platPublicKey);//验签：使用平台公钥
$secretKey->setSecKeyEncryptKey($platPublicKey);//sec_key加密：使用平台公钥
$secretKey->setSecKeyDecryptKey($mchPrivateKey);//sec_key解密：使用商户私钥

$url = "https://api.joinpay.com/fastpay";
try {
    $response = RequestUtil::doRequest($url, $request, $secretKey);

   // $response = new Response();
  // print_r( $response);
    if ($response->isSuccess()) {//受理成功
        $dataArr = json_decode($response->getData(), true);
        
        // var_dump($dataArr);

       
           // echo "FAIL OR PROCESSING OR UNKNOWN, Response = ";
           $res=$response->getData();
           $arrs=json_decode($res,true);
          // print_r( $response);
           
          if(empty( $arrs['err_msg'])){
            echo "发送成功";
          }else{
            echo $arrs['err_msg'];
          }
            
    }else{

     // $dataArrx = json_decode($response, true);
       echo $response->getBizMsg();
      
     }
} catch (Exception $e) {
    print_r($e);
}

